﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Data;
using System.ComponentModel;


namespace postSale.Controls
{
    public class gridMaintenanceDetail : common.controls.baseDataGridView 
    {
        public enum ColumnNames
        {
            itemCode, model , itemName, warrantyInMonths, warrantyEndDate, techSpec, country, notes 
        };

        public gridMaintenanceDetail()
        {
            this.myAutoFitColumn = ColumnNames.itemName.ToString();
            this.AutoArrange();
            this.myOnFind += new OnCellEvent(FindData);
        }

        public void UpdateItemList(string itemCode)
        {
            if (itemCode == null)
            {
                for (int idx = 0; idx < this.Rows.Count; idx++)
                {
                    if (this.Rows[idx].Cells[ColumnNames.itemCode.ToString()].Value == null) continue;
                    DataAccess.DataCache.FindITEM(this.Rows[idx].Cells[ColumnNames.itemCode.ToString()].Value.ToString());
                }
            }
            else DataAccess.DataCache.FindITEM(itemCode);
        }
        public override void Init(BindingSource dataSource)
        {
            base.Init(dataSource);

            data.postSaleDS.maintenanceDetailDataTable detailTbl = (data.postSaleDS.maintenanceDetailDataTable)dataSource.DataSource;
            
            // 
            // itemCode
            // 
            common.controls.DataGridViewTextBoxColumnExt itemCode = new common.controls.DataGridViewTextBoxColumnExt();
            itemCode.DataPropertyName = detailTbl.itemCodeColumn.ColumnName;
            itemCode.HeaderText = "Mã.hàng";
            itemCode.Name = ColumnNames.itemCode.ToString();
            itemCode.Width = 90;
            itemCode.Uppercase = true;

            // 
            // model
            // 
            common.controls.DataGridViewTextBoxColumnExt model = new common.controls.DataGridViewTextBoxColumnExt();
            model.DataPropertyName = detailTbl.modelColumn.ColumnName;
            model.HeaderText = "Model";
            model.Name = ColumnNames.model.ToString();
            model.Width = 100;
            model.Uppercase = true;

            // 
            // Country
            // 
            DataAccess.DataCache.LoadCOUNTRY();
            BindingSource countrySource = new BindingSource();
            countrySource.DataSource = DataAccess.DataCache.myMasterDS.countryCode;

            DataGridViewComboBoxColumn country = new DataGridViewComboBoxColumn();
            country.DataPropertyName = detailTbl.countryColumn.ColumnName;
            country.HeaderText = "Xuất xứ";
            country.Name = ColumnNames.country.ToString();
            country.Width = 110;
            country.DataSource = countrySource;
            country.DisplayMember = DataAccess.DataCache.myMasterDS.countryCode.descriptionColumn.ColumnName;
            country.ValueMember = DataAccess.DataCache.myMasterDS.countryCode.codeColumn.ColumnName;
            // 
            // itemName
            // 
            BindingSource itemSource = new BindingSource();
            itemSource.DataSource = DataAccess.DataCache.myMasterDS.Item;
            DataGridViewComboBoxColumn itemName = new DataGridViewComboBoxColumn();
            itemName.DataSource = itemSource;
            itemName.DataPropertyName = detailTbl.itemCodeColumn.ColumnName;
            itemName.DisplayMember = DataAccess.DataCache.myMasterDS.Item.nameColumn.ColumnName;
            itemName.ValueMember = DataAccess.DataCache.myMasterDS.Item.itemCodeColumn.ColumnName;
            itemName.HeaderText = "Diễn giải";
            itemName.Name = ColumnNames.itemName.ToString();
            itemName.ReadOnly = true;
            
            itemName.DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing;
            itemName.Width = 170;
            itemName.DefaultCellStyle.BackColor = CommonTypes.Settings.sysColorDisableBG;
            itemName.DefaultCellStyle.ForeColor = CommonTypes.Settings.sysColorDisableFG;

            // 
            // techSpec
            // 
            DataGridViewTextBoxColumn techSpec = new DataGridViewTextBoxColumn();
            techSpec.DataPropertyName = detailTbl.techSpecColumn.ColumnName;
            techSpec.HeaderText = "Thông tin kỹ thuật";
            techSpec.Name = ColumnNames.techSpec.ToString();
            techSpec.Width = 200;

            // 
            // warrantyInMonths
            // 
            DataGridViewCellStyle monthCellStyle = new DataGridViewCellStyle();
            monthCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            monthCellStyle.Format = "N" + common.SysLibs.GetPrecisionFromMask(CommonTypes.Settings.sysMaskWarranty).ToString();

            DataGridViewTextBoxColumn warrantyInMonths = new DataGridViewTextBoxColumn();
            warrantyInMonths.DataPropertyName = detailTbl.warrantyInMonthsColumn.ColumnName;
            warrantyInMonths.HeaderText = "Tháng";
            warrantyInMonths.Name = ColumnNames.warrantyInMonths.ToString();
            warrantyInMonths.Width = 50;
            warrantyInMonths.DefaultCellStyle = monthCellStyle;

            // 
            // warrantyEndDate
            // 
            DataGridViewCellStyle dateCellStyle = new DataGridViewCellStyle();
            dateCellStyle.Format = "d"; 
            dateCellStyle.NullValue = null;

            DataGridViewTextBoxColumn warrantyEndDate = new DataGridViewTextBoxColumn();
            warrantyEndDate.DataPropertyName = detailTbl.warrantyEndDateColumn.ColumnName;
            warrantyEndDate.HeaderText = "Đến ngày";
            warrantyEndDate.Name = ColumnNames.warrantyEndDate.ToString();
            warrantyEndDate.Width = 85;
            warrantyEndDate.DefaultCellStyle = dateCellStyle;

            // 
            // Notes
            // 
            DataGridViewTextBoxColumn notes = new DataGridViewTextBoxColumn();
            notes.DataPropertyName = detailTbl.noteColumn.ColumnName;
            notes.HeaderText = "Ghi chú";
            notes.Name = ColumnNames.notes.ToString();
            notes.Width = 110;

            // 
            // myDetailGrid
            // 
            this.Visible = true;
            this.DataSource = dataSource;

            this.ReadOnly = false;
            this.ScrollBars = ScrollBars.Vertical;
            this.AutoGenerateColumns = false;
            this.Columns.Clear();
            this.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
                    itemCode,model,itemName,techSpec,warrantyInMonths,warrantyEndDate, country,notes});
            this.myAutoFitColumn = ColumnNames.itemName.ToString();
            this.DisbaleReadOnlyColumns();

        }
        protected override void OnDefaultValuesNeeded(DataGridViewRowEventArgs e)
        {
            base.OnDefaultValuesNeeded(e);
            if (e == null) return;
            if (myDataSource.Current != null)
                DataAccess.DataLibs.InitData((data.postSaleDS.maintenanceDetailRow)((DataRowView)myDataSource.Current).Row);
        }
        
        public override void Refresh()
        {
            if (IsOnProcessing) return;
            try
            {
                IsOnProcessing = true;
                UpdateItemList(null);
                IsOnProcessing = false;
                base.Refresh();
            }
            catch (Exception er)
            {
                IsOnProcessing = false;
                ErrorHandler(this, er);
            }
        }

        public void FindData(object sender, common.controls.GridEventArgs arg)
        {
            string findCode = this.Rows[arg.RowIndex].Cells[arg.ColumnIndex].EditedFormattedValue.ToString(); ;
            if (this.CurrentCell == null) return;
            //Code
            if (this.Columns[arg.ColumnIndex].Name.Trim() == ColumnNames.itemCode.ToString())
            {
                application.dataFind.myItemFind.Find(findCode, true);
                data.masterDS.ItemRow itemRow = application.dataFind.myItemFind.selectedDataRow;
                if (itemRow != null)
                {
                    this.Rows[arg.RowIndex].Cells[arg.ColumnIndex].Value = itemRow.itemCode;
                    UpdateItemList(itemRow.itemCode);
                }
                else this.Rows[arg.RowIndex].Cells[arg.ColumnIndex].Value = DBNull.Value;
                this.myDataSource.ResetBindings(false);
                this.myDataSource.EndEdit(); 
            }
        }
    }
}
